# mysql测试
import time

from sqlalchemy import create_engine, DATETIME
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, BIGINT, String, VARCHAR, INT, SMALLINT, Enum
from sqlalchemy.orm import sessionmaker
from my_model import HkBasePoint


class MySqlHelper(object):
    def __init__(self):
        url = "mysql+pymysql://root:123456@127.0.0.1:3307/byd_andon_xian"
        self.engine = create_engine(url, max_overflow=5)

    # result = engine.execute('select * from hk_device_channel')
    # for item in result.fetchall():
    #     print(item)

    print("-------------------------------------------------------------------")

    def get_session(self):
        Session = sessionmaker(bind=self.engine, autoflush=True, autocommit=True)
        return Session()


helper = MySqlHelper()

if __name__ == '__main__':
    while True:
        try:
            s = helper.get_session()
            ret = s.query(HkBasePoint).order_by(HkBasePoint.id)[1:3]
            for item in ret:
                print(item.id, item.point_name, item.device_channel_id)
            print("------------------------------------------")
            time.sleep(3)
        except Exception as e:
            print(e)
            time.sleep(2)
